---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[


]]--
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 素材
    local assets =
    {
        Asset("ANIM", "anim/loramia_weapon_evoluted_swiftstrike_creation.zip"),
        Asset( "IMAGE", "images/inventoryimages/loramia_weapon_evoluted_swiftstrike_creation_tool.tex" ),
        Asset( "ATLAS", "images/inventoryimages/loramia_weapon_evoluted_swiftstrike_creation_tool.xml" ),
        Asset( "IMAGE", "images/inventoryimages/loramia_weapon_evoluted_swiftstrike_creation_weapon.tex" ),
        Asset( "ATLAS", "images/inventoryimages/loramia_weapon_evoluted_swiftstrike_creation_weapon.xml" ),
    }
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 参数
    local DAMAGE = 100
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 类型    
    local function GetCurrentType(inst)
        return inst.components.loramia_data:Get("type") or "weapon"
    end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 穿戴
    local function onequip(inst, owner)
        -- owner.AnimState:OverrideSymbol("swap_object", "swap_cane", "swap_cane")
        owner.AnimState:ClearOverrideSymbol("swap_object")
        owner.AnimState:Show("ARM_carry")
        owner.AnimState:Hide("ARM_normal")

        local fx = SpawnPrefab("loramia_weapon_evoluted_swiftstrike_creation_fx")
        fx.entity:SetParent(owner.entity)
        fx.entity:AddFollower()
        fx.Follower:FollowSymbol(owner.GUID, "swap_object",0,0,0,true)
        inst.in_hand_fx = fx
        if GetCurrentType(inst) == "weapon" then
            fx.AnimState:PlayAnimation("in_hand_weapon")
        else
            fx.AnimState:PlayAnimation("in_hand_tool")
        end
    end

    local function onunequip(inst, owner)
        owner.AnimState:ClearOverrideSymbol("swap_object")
        owner.AnimState:Hide("ARM_carry")
        owner.AnimState:Show("ARM_normal")

        if inst.in_hand_fx then
            inst.in_hand_fx:Remove()
            inst.in_hand_fx = nil
        end

    end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 状态切换
    local function change_2_tool(inst)
        inst.components.loramia_data:Set("type","tool")
        inst.components.weapon:SetDamage(0)
        if inst.components.tool == nil then
            inst:AddComponent("tool")
        end
        inst.components.tool:SetAction(ACTIONS.CHOP, 6)
        inst.components.tool:SetAction(ACTIONS.MINE, 6)
        inst.components.tool:SetAction(ACTIONS.HAMMER, 6)
        -- inst.components.tool:SetAction(ACTIONS.DIG, 6)
        inst.components.tool:EnableToughWork(true)

        inst.components.inventoryitem.imagename = "loramia_weapon_evoluted_swiftstrike_creation_tool"
        inst.components.inventoryitem.atlasname = "images/inventoryimages/loramia_weapon_evoluted_swiftstrike_creation_tool.xml"
        inst:PushEvent("imagechange")

        inst.AnimState:PlayAnimation("idle_tool")
        if inst.in_hand_fx then
            inst.in_hand_fx.AnimState:PlayAnimation("in_hand_tool")
        end

    end
    local function change_2_weapon(inst)
        inst.components.loramia_data:Set("type","weapon")
        inst.components.weapon:SetDamage(DAMAGE)
        if inst.components.tool ~= nil then
            inst:RemoveComponent("tool")
        end

        inst.components.inventoryitem.imagename = "loramia_weapon_evoluted_swiftstrike_creation_weapon"
        inst.components.inventoryitem.atlasname = "images/inventoryimages/loramia_weapon_evoluted_swiftstrike_creation_weapon.xml"
        inst:PushEvent("imagechange")

        inst.AnimState:PlayAnimation("idle_weapon")
        if inst.in_hand_fx then
            inst.in_hand_fx.AnimState:PlayAnimation("in_hand_weapon")
        end
    end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- workable
    local function workable_com_install(inst)
        inst:ListenForEvent("Loramia_OnEntityReplicated.loramia_com_workable",function(inst,replica_com)
            replica_com:SetTestFn(function(inst,doer,right_click)
                -- return inst.replica.inventoryitem:IsGrandOwner(doer)
                return doer.replica.combat:GetWeapon() == inst
            end)
            replica_com:SetText("loramia_weapon_evoluted_swiftstrike_creation",STRINGS.ACTIONS.CYCLE.GENERIC)
            -- replica_com:SetSGAction("doshortaction")
        end)
        if not TheWorld.ismastersim then
            return
        end
        inst:AddComponent("loramia_com_workable")
        inst.components.loramia_com_workable:SetOnWorkFn(function(inst)
            if GetCurrentType(inst) == "weapon" then
                change_2_tool(inst)
            else
                change_2_weapon(inst)
            end
            return true
        end)
    end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---- 攻击
    local function on_attack_fn(inst)
        if GetCurrentType(inst) ~= "weapon" then
            inst:PushEvent("type","weapon")
        end
    end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---- 武器
    local function fn()
        local inst = CreateEntity()

        inst.entity:AddTransform()
        inst.entity:AddAnimState()
        inst.entity:AddSoundEmitter()
        inst.entity:AddNetwork()

        MakeInventoryPhysics(inst)

        inst.AnimState:SetBank("loramia_weapon_evoluted_swiftstrike_creation")
        inst.AnimState:SetBuild("loramia_weapon_evoluted_swiftstrike_creation")
        inst.AnimState:PlayAnimation("idle_weapon")

        --weapon (from weapon component) added to pristine state for optimization
        inst:AddTag("weapon")

        MakeInventoryFloatable(inst, "med", 0.05, {0.85, 0.45, 0.85})


        inst.entity:SetPristine()

        workable_com_install(inst)

        if not TheWorld.ismastersim then
            return inst
        end

        ----------------------------------------------------------------------
        --- 切换
            inst:ListenForEvent("type",function(_,_type)
                if _type == "weapon" then
                    change_2_weapon(inst)
                elseif _type == "tool" then
                    change_2_tool(inst)
                end
            end)
        ----------------------------------------------------------------------
        --- 数据库
            inst:AddComponent("loramia_data")
            inst:DoTaskInTime(0,function()
                inst:PushEvent("type",GetCurrentType(inst))
            end)

        ----------------------------------------------------------------------
        --- 武器
            inst:AddComponent("weapon")
            inst.components.weapon:SetDamage(DAMAGE)
            inst.components.weapon:SetRange(2)
            inst.components.weapon:SetOnAttack(on_attack_fn)
        ----------------------------------------------------------------------
        --- 检查
            inst:AddComponent("inspectable")
            inst:AddComponent("inventoryitem")
            inst.components.inventoryitem.imagename = "loramia_weapon_evoluted_swiftstrike_creation_weapon"
            inst.components.inventoryitem.atlasname = "images/inventoryimages/loramia_weapon_evoluted_swiftstrike_creation_weapon.xml"
        ----------------------------------------------------------------------
        --- 装备
            inst:AddComponent("equippable")
            inst.components.equippable:SetOnEquip(onequip)
            inst.components.equippable:SetOnUnequip(onunequip)
        ----------------------------------------------------------------------
        --- 耐久
            inst:AddComponent("finiteuses")
            inst.components.finiteuses:SetOnFinished(inst.Remove)            
        ----------------------------------------------------------------------
        --- 作祟
            MakeHauntableLaunch(inst)
        ----------------------------------------------------------------------
        --- 落水影子
            local function shadow_init(inst)
                if inst:IsOnOcean(false) then       --- 如果在海里（不包括船）
                    -- inst.AnimState:Hide("SHADOW")
                    if GetCurrentType(inst) == "weapon" then
                        inst.AnimState:PlayAnimation("water_weapon")
                    else
                        inst.AnimState:PlayAnimation("water_tool")
                    end
                else                                
                    -- inst.AnimState:Show("SHADOW")
                end
            end
            inst:ListenForEvent("on_landed",shadow_init)
            shadow_init(inst)
        ----------------------------------------------------------------------
        return inst
    end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---
    local function fx()
        local inst = CreateEntity()

        inst.entity:AddTransform()
        inst.entity:AddAnimState()
        inst.entity:AddSoundEmitter()
        inst.entity:AddNetwork()

        inst.AnimState:SetBank("loramia_weapon_evoluted_swiftstrike_creation")
        inst.AnimState:SetBuild("loramia_weapon_evoluted_swiftstrike_creation")
        inst.AnimState:PlayAnimation("in_hand_weapon")

        inst:AddTag("FX")
        inst:AddTag("fx")
        inst:AddTag("INLIMBO")

        inst.entity:SetPristine()

        if not TheWorld.ismastersim then
            return inst
        end

        return inst
    end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

return Prefab("loramia_weapon_evoluted_swiftstrike_creation", fn, assets),
    Prefab("loramia_weapon_evoluted_swiftstrike_creation_fx", fx, assets)
